<!--  -->
<template>
  <div class="system-list-data-table">
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="系统名称" width="230">
      </el-table-column>
      <el-table-column prop="pages" label="挂载页面">
      </el-table-column>
      <el-table-column prop="systemType.name" label="类型" width="180">
      </el-table-column>
      <el-table-column prop="statistics" label="统计" width="250">
      </el-table-column>
      <el-table-column label="操作" width="480">
        <template slot-scope="scope">
          <el-button @click="categoryManage(scope.row.id)" type="success" size="small"
            v-if="[1,3,5,6,8].indexOf(scope.row.systemType.id) > -1">分类管理</el-button>
            <el-button @click="downloadAttributeSet(scope.row.id)" type="info" size="small"
            v-if="[5].indexOf(scope.row.systemType.id) > -1">属性设置</el-button>
             <el-button @click="informationAttributeSet(scope.row.id)" type="info" size="small"
            v-if="[7].indexOf(scope.row.systemType.id) > -1">属性设置</el-button>
            <el-button @click="pdtableAttributeSet(scope.row.id)" type="info" size="small"
            v-if="[8].indexOf(scope.row.systemType.id) > -1">属性设置</el-button>
          <el-button @click="listMagage(scope.row.id)" type="warning " size="small">内容管理</el-button>
          <el-button @click="setupSystem(scope.row.id)" type="primary " size="small">系统设置</el-button>
          <el-button @click="editorSystem(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteSystem(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 创建编辑系统 -->
    <SaveSystem ref="editSystem" :resetData="resetData" :tableData="tableData" />
    <DialogSystem />
  </div>
</template>

<script>
import SaveSystem from '@/components/main/user/system/common/SaveSystem.vue'
import { getSystemCategoryPatjBySid, getSystemListPathByItem, getSystemDownloadAttrBySid, getSystemInformationAttrBySid, getSystemPdtableAttrBySid } from '@/core/functions.js'
import * as WindowType from '@/config/windowtype.js'
import PubSub from 'pubsub-js'

import DialogSystem from '@/components/common/design/dialog/system/DialogSystem.vue'
export default {
  props: {
    dataTotal: {
      type: Function
    }
  },
  created () {
    this.initData()
  },
  data () {
    return {
      loading: true,
      // 表格数据
      tableData: [],
      // 请求返回的数据
      data: []
    }
  },
  watch: {
    tableData: {
      handler (newValue) {
        let total = newValue && newValue.length ? newValue.length : 0
        this.dataTotal(total)
      },
      deep: true
    }
  },
  methods: {
    initData () {
      this.loading = true
      this.$post('/system/table-infos').then((response) => {
        this.loading = false
        if (response && response.length > 0) {
          this.data = response
          this.tableData = makeInitData(response)
        } else {
          this.data = []
          this.tableData = noData()
        }
      })
    },
    resetData () {
      this.initData()
    },
    getTableData () {
      return this.tableData
    },
    // 分类管理
    categoryManage (id) {
      let fullPath = getSystemCategoryPatjBySid(id)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    // 下载系统属性设置
    downloadAttributeSet (id) {
      let fullPath = getSystemDownloadAttrBySid(id)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    // 信息系统属性设置
    informationAttributeSet (id) {
      let fullPath = getSystemInformationAttrBySid(id)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    // 产品表格系统属性设置
    pdtableAttributeSet (id) {
      let fullPath = getSystemPdtableAttrBySid(id)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    // 内容列表管理
    listMagage (id) {
      let item = getItem(id, this.data)
      if (!item) {
        return
      }
      let fullPath = getSystemListPathByItem(item)
      if (!fullPath) {
        return
      }
      this.$store.dispatch('navAppend', { path: fullPath })
    },
    editorSystem (id) {
      let data
      for (let i in this.data) {
        if (this.data[i].id === id) {
          data = this.data[i]
        }
      }
      data.systemTypeId = data.systemType.id
      this.$refs['editSystem'].show('编辑系统-' + data.name, data)
    },
    deleteSystem (id) {
      let data
      for (let i in this.tableData) {
        if (this.tableData[i].id === id) {
          data = this.tableData[i]
        }
      }

      this.$confirm('删除系统会删除该系统下的所有数据，确认要删除“' + data.name + '”下的所有数据？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.$post('/system/delete', {
            id: id,
            name: data.name
          }).then((response) => {
            if (response && response.error) {

            } else if (!isNaN(parseInt(response)) && parseInt(response) === id) {
              this.resetData()
            }
          })
        })
        .catch(() => {
        })
    },
    // 系统设置
    setupSystem (id) {
      let item = this.tableData.find(item => {
        return parseInt(item.id) === parseInt(id)
      })
      if (!item) {
        return
      }
      let systemTypeId = item.systemType.id
      switch (systemTypeId) {
        case 1:
          PubSub.publish(WindowType.EDIT_ARTICLE_SYSTEM_CONFIG, {systemId: id, curpageName: false})
          break
        case 3:
          PubSub.publish(WindowType.EDIT_PRODUCT_SYSTEM_CONFIG, {systemId: id, curpageName: false})
          break
        case 5:
          PubSub.publish(WindowType.EDIT_DOWNLOAD_SYSTEM_CONFIG, {systemId: id, curpageName: false})
          break
        case 6:
          PubSub.publish(WindowType.EDIT_FAQ_SYSTEM_CONFIG, {systemId: id, curpageName: false})
          break
        case 7:
          PubSub.publish(WindowType.EDIT_INFORMATION_SYSTEM_CONFIG, {systemId: id, curpageName: false})
          break
        case 8:
          PubSub.publish(WindowType.EDIT_PDTABLE_SYSTEM_CONFIG, {systemId: id, curpageName: false})
          break

        default:
          break
      }
    }
  },
  components: {
    SaveSystem,
    DialogSystem
  }
}

function noData () {
  return []
}

/**
 * {
        id: 0,
        name: '产品系统',
        pages: '产品中心、客户案例产品中心、客户案例产品中心、客户案例产品中心、客户案例产品中心、客户案例',
        statistics: '分类个数：1125， 内容个数：15511',
        systemType: {}
      }
 */
function makeInitData (data) {
  let result = []
  for (let i in data) {
    let item = data[i]
    result.push({
      id: item.id,
      name: item.name,
      pages: item.pages,
      statistics: item.statistics,
      systemType: item.systemType
    })
  }
  return result
}

function getItem (id, list) {
  let data
  for (let i in list) {
    if (list[i].id === id) {
      data = list[i]
      return data
    }
  }
  return null
}

</script>

<style scoped>
.system-list-data-table /deep/ .el-table th {
  color: #555;
}
.system-list-data-table .el-button--small {
  padding: 9px 15px;
}
.system-list-data-table .el-button--text {
  padding-left: 0;
  padding-right: 0;
}
</style>
